package config

import (
	"github.com/spf13/viper"
	"time"
)

type VmrSettings struct {
	Host        string
	AccessKey   string
	AccessToken string
}

type ServerSettings struct {
	HttpPort     string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

type AppSettings struct {
	DefaultPageSize int
	MaxPageSize     int
	LogFileName     string
	LogFileExt      string
}

type DatabaseSettings struct {
	UpgradeFolder string
}

type Setting struct {
	vp *viper.Viper
}

func NewSetting(env string) (*Setting, error) {
	//创建viper实例
	vp := viper.New()
	vp.AddConfigPath("config/")
	vp.SetConfigName("config-" + env)
	//指定配置文件格式
	vp.SetConfigType("yml")
	//读取配置文件
	err := vp.ReadInConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}

func (s *Setting) ReadSection(k string, v interface{}) error {
	err := s.vp.UnmarshalKey(k, v)
	if err != nil {
		return err
	}
	return nil
}

//type BoxS3Config struct {
//	EndPoint      string
//	AccessKey     string
//	SecretKey     string
//	DefaultBucket string
//}
//
//type PlatformS3Config struct {
//	EndPoint      string
//	AccessKey     string
//	SecretKey     string
//	DefaultBucket string
//}
//
//type KafkaConfig struct {
//	Servers       string
//	SaasPushTopic string
//}
//
type Config struct {
	BaseConfig   `ini:"BASE_CONFIG"`
	SqliteConfig `ini:"SQLITE"`
	LogConfig    `ini:"LOG"`
}

type SqliteConfig struct {
	SqlitePath    string `ini:"SQLITE_PATH"`
	UpgradeFolder string `ini:"UPGRADE_FOLDER"`
}

type LogConfig struct {
	LogSaveUrl  string `ini:"LOG_SAVE_URL"`
	LogSaveName string `ini:"LOG_SAVE_NAME"`
}

type BaseConfig struct {
	HttpPort     int    `ini:"HTTP_PORT"`
	ReadTimeout  int    `ini:"READ_TIMEOUT"`
	WriteTimeout int    `ini:"WRITE_TIMEOUT"`
	VmrHost      string `ini:"VMR_HOST"`
}

//func InitConfig(env string) *Config {
//	fmt.Println("baseConfig.env：", env)
//
//	config := &Config{}
//	path := getAppPath() + "/config/" + env + ".ini"
//	err := ini.MapTo(config, path)
//	if err != nil {
//		fmt.Println("ini文件映射出错啦，错误是:", err)
//	}
//
//	fmt.Println(config.BaseConfig)
//	//fmt.Println(config, config.BaseConfig, config.SqliteConfig, config.LogConfig)
//	return config
//}
//
//// 获取当前路径
//func getAppPath() string {
//	var _, b, _, _ = runtime.Caller(0)
//	return filepath.Join(filepath.Dir(b), "../")
//}
